package com.gaobug.article.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gaobug.base.utils.redis.RedisKeyUtils;
import com.gaobug.base.enums.StatusEnum;
import com.gaobug.base.utils.redis.RedisUtil;
import com.gaobug.article.domain.LikeInfo;
import com.gaobug.article.dto.LikedCountDTO;
import com.gaobug.article.service.LikeService;
import com.gaobug.article.dao.LikeMapper;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author huang
 * @description 针对表【t_like(博客点赞记录表)】的数据库操作Service实现
 * @createDate 2022-07-02 12:50:44
 */
@Service
public class LikeServiceImpl extends ServiceImpl<LikeMapper, LikeInfo> implements LikeService {
    @Override
    public void saveLiked2Redis(String likedUserId, String likedPostId) {
        String key = RedisKeyUtils.getLikedKey(likedUserId, likedPostId,"");
        RedisUtil.hPut(RedisKeyUtils.USER_COMMENT_LIKED, key, StatusEnum.LIKE.getCode());
    }

    @Override
    public void unlikeFromRedis(String likedUserId, String likedPostId) {
        String key = RedisKeyUtils.getLikedKey(likedUserId, likedPostId,"");
        RedisUtil.hPut(RedisKeyUtils.USER_COMMENT_LIKED, key, StatusEnum.UNLIKE.getCode());
    }

    @Override
    public void deleteLikedFromRedis(String likedUserId, String likedPostId) {
        String key = RedisKeyUtils.getLikedKey(likedUserId, likedPostId,"");
        RedisUtil.hDelete(RedisKeyUtils.USER_COMMENT_LIKED, key);
    }

    @Override
    public void incrementLikedCount(String likedUserId) {
        RedisUtil.hIncrBy(RedisKeyUtils.USER_ARTICLE_LIKED_COUNT, likedUserId, 1);
    }

    @Override
    public void decrementLikedCount(String likedUserId) {
        RedisUtil.hIncrBy(RedisKeyUtils.USER_ARTICLE_LIKED_COUNT, likedUserId, -1);
    }

    @Override
    public List<LikeInfo> getLikedDataFromRedis() {
        Cursor<Map.Entry<Object, Object>> cursor = RedisUtil.hScan(RedisKeyUtils.USER_COMMENT_LIKED, ScanOptions.NONE);
        List<LikeInfo> list = new ArrayList<>();
        while (cursor.hasNext()) {
            Map.Entry<Object, Object> entry = cursor.next();
            String key = (String) entry.getKey();
            //组装成 UserLike 对象

            //存到 list 后从 Redis 中删除
        }
        // 批量保存

        return list;
    }

    @Override
    public List<LikedCountDTO> getLikedCountFromRedis() {
        Cursor<Map.Entry<Object, Object>> cursor = RedisUtil.hScan(RedisKeyUtils.USER_ARTICLE_LIKED_COUNT, ScanOptions.NONE);
        List<LikedCountDTO> list = new ArrayList<>();
        while (cursor.hasNext()) {
            Map.Entry<Object, Object> map = cursor.next();
            //将点赞数量存储在 LikedCountDT
            String key = (String) map.getKey();
//            LikedCountDTO dto = new LikedCountDTO(key, (Integer) map.getValue());
//            list.add(dto);
//            //从Redis中删除这条记录
//            redisTemplate.opsForHash().delete(RedisKeyUtils.MAP_KEY_USER_LIKED_COUNT, key);
        }
        return list;
    }
}




